package org.kpli.jak.net;

import java.io.*;

import java.net.*;

import java.util.*;

public class Server {

	public static void main(String[] args) {

		new Server();

	}

	public Server() {

		try {

			// Membuat server socket dengan port 8000

			ServerSocket serverSocket = new ServerSocket(8000);

			System.out.print("Server started at " + new Date() + '\n');

			// Membaca request koneksi dari client

			Socket socket = serverSocket.accept();

			// Membuat data input stream dan data output stream

			DataInputStream inputFromClient = new DataInputStream(

			socket.getInputStream());

			DataOutputStream outputToClient = new DataOutputStream(

			socket.getOutputStream());

			do {

				while (true) {

					// Menerima Double yang dikirimkan oleh client

					double radius = inputFromClient.readDouble();

					// Menghitung Hasil

					double area = radius * radius * Math.PI;

					// Mengirim hasil histungan ke client

					outputToClient.writeDouble(area);

					System.out.print("Radius received from client: " + radius
							+ '\n');

					System.out.print("Area found: " + area + '\n');

				}

			} while (inputFromClient.readDouble() != 100);

		}

		catch (IOException ex) {

			System.err.println(ex);

		}

	}
}
